<?php
namespace User\Controller;
use Common\Controller\BaseController;
use Think\Hook;

class LoginController extends BaseController {
    protected $config = array(
        'login_auth'=>false,
        'ctrl_auth'=>false,
    );
    public function index() {
        if(IS_POST) {
            $this->doLogin();
        }
        $this->_web('title', '用户登录');
        $this->display();
    }
    protected function doLogin(){
        $post = I('post.');
        $UserDao = D('Common/User');
        // 登录开始
        Hook::listen('login_start', $post);
        if($post['remember']) {
            $res = $UserDao->cookieLogin($post['username'], $post['password']);
        }else $res = $UserDao->sessionLogin($post['username'], $post['password']);
        if(!$res) {
            $this->error($UserDao->getError());
        }
        // 登录工作完成
        Hook::listen('login_end', $res);
        $this->success('登录成功', U('Admin/Index/index'));
    }
    public function logout() {
        // 注销工作 开始
        Hook::listen('logout_start');
        $user_auth = D('Common/User')->getLocalSession();
        $res = D('Common/User')->clearSession($user_auth['id']);
        // 注销工作 完成
        Hook::listen('logout_end', $res);
        if($res){
            D('Common/User')->clearLocalSession();// 本地会话一同清除
            $this->success('已退出登录', U('User/Login/index'));
        }else {
            $this->error('帐号注销失败');
        }
    }
}